package com.system.web.servlet;

import com.system.dao.UserDao;
import com.system.vo.User;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet
{
  private static final long serialVersionUID = 2415380891617248217L;

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    UserDao dao = new UserDao();
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    User user = dao.checkLogin(username, password);
    if (user != null)
    {
      request.getSession().setAttribute("session_user", user);
      response.sendRedirect("home.jsp");
    } else {
      request.setAttribute("error", "用户名或密码错误");
      request.getRequestDispatcher("login.jsp").forward(request, response);
    }
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    doPost(request, response);
  }

  public void init()
    throws ServletException
  {
  }

  public void destroy()
  {
  }
}